/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-01 22:19:08
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.edu.service.impl;

import javax.transaction.Transactional;

import com.joysuch.wwyt.bp.entity.BpDepart;
import com.joysuch.wwyt.bp.entity.BpJob;
import com.joysuch.wwyt.bp.repository.BpDepartDao;
import com.joysuch.wwyt.bp.repository.BpJobDao;
import com.joysuch.wwyt.core.entity.BaseUser;
import com.joysuch.wwyt.core.exception.BusinessRestrictionsException;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.edu.bean.EduExamUserScopeDetailBean;
import com.joysuch.wwyt.edu.entity.EduExamUserScope;
import com.joysuch.wwyt.edu.mapper.EduExamUserScopeMapper;
import com.joysuch.wwyt.edu.repository.EduExamUserScopeDao;
import com.joysuch.wwyt.edu.service.EduExamUserScopeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
public class EduExamUserScopeServiceImpl implements EduExamUserScopeService {

    @Autowired
    private EduExamUserScopeDao dao;

    @Autowired
    private EduExamUserScopeMapper eduExamUserScopeMapper;

    @Autowired
    private BpJobDao jobDao;

    @Autowired
    private BaseUserDao baseUserDao;

    @Autowired
    private BpDepartDao bpDepartDao;

    @Override
    public void delete(Long id) throws BusinessRestrictionsException {
        // 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用。不能删除时将抛出业务异常
        checkBussinessRestrictions(id);
        dao.deleteById(id);
    }

    /**
     * 校验业务逻辑是否允许删除。如本数据被其他业务关联，删除后导致其他业务不可用
     *
     * @param id 要删除的业务ID
     * @throws BussinessRestrictionsException 抛出的业务异常
     */
    private void checkBussinessRestrictions(Long id) throws BusinessRestrictionsException {

    }

    @Override
    public EduExamUserScope findById(Long id) {
        return dao.findById(id).orElse(null);
    }

    @Override
    public EduExamUserScopeDetailBean getById(Long id) {
        EduExamUserScope entity = findById(id);
        if (entity == null) {
            return null;
        }
        EduExamUserScopeDetailBean bean = new EduExamUserScopeDetailBean();
        BeanUtils.copyProperties(entity, bean);
        return bean;
    }

    @Override
    public void scopeUpdate() {
        List<EduExamUserScope> eduExamUserScopes = eduExamUserScopeMapper.scopeUpdate();
        List<EduExamUserScope> examUserScopes = new ArrayList<>();
        if (!eduExamUserScopes.isEmpty()) {
            for (EduExamUserScope eduExamUserScope : eduExamUserScopes) {
                List<BaseUser> baseUsers = new ArrayList<>();
                if (eduExamUserScope.getObjId() != null && eduExamUserScope.getObjType().equals("J")) {
                    BpJob one = jobDao.getOne(eduExamUserScope.getObjId());
                    baseUsers = baseUserDao.getJpbId(one.getId());
                }
                if (eduExamUserScope.getObjId() != null && eduExamUserScope.getObjType().equals("D")) {
                    BpDepart one = bpDepartDao.getOne(eduExamUserScope.getObjId());
                    baseUsers = baseUserDao.getPartId(one.getId());
                }
                for (BaseUser baseUser : baseUsers) {
                    EduExamUserScope examUserScope = new EduExamUserScope();
                    BeanUtils.copyProperties(eduExamUserScope, examUserScope);
                    examUserScope.setObjId(baseUser.getId());
                    examUserScope.setObjType("U");
                    examUserScope.setId(null);
                    examUserScopes.add(examUserScope);
                }
                eduExamUserScope.setDeleteFlag("1");
                dao.save(eduExamUserScope);
            }
            ArrayList<EduExamUserScope> collect = examUserScopes.stream().collect(
                    Collectors.collectingAndThen(
                            Collectors.toCollection(
                                    () -> new TreeSet<>(
                                            Comparator.comparing(
                                                    o -> o.getExamId() + ";" + o.getObjId()))), ArrayList::new));
            eduExamUserScopeMapper.saveAll(collect);
        }
    }
}
